﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.Specialized;
using System.Xml.Serialization;
using Ppt = Microsoft.Office.Interop.PowerPoint;
using System.Collections.ObjectModel;
using DataFactory;

namespace ObjectModel.PowerPoint.New
{
    //public class PresentationConstants
    //{
    //    const string XML_ROOT_NAME = "Constants";

    //    public PPTSerializableCollection<Constant> Constants { get; set; }

    //    public PresentationConstants(Ppt.Presentation pres)
    //    {
    //        Constants = PPTSerializableCollection<Constant>.DeserializeFromPresentation(pres, XML_ROOT_NAME);
    //        if (Constants == null)
    //            Constants = new PPTSerializableCollection<Constant>();

    //        Globals.PresentationEvents[pres].PresentationBeforeSave +=
    //            (o, e) => { Constants.SerializeToPresentation(pres, XML_ROOT_NAME); };
    //    }
    //}

    public class PresentationConstants
    {
        const string XML_ROOT_NAME = "Constants";
        readonly PPTSerializer mSerializer = new PPTSerializer();

        public ConstantsCollection Constants { get; set; }

        public PresentationConstants(Ppt.Presentation pres)
        {
            Constants = PPTSerializer.Deserialize<ConstantsCollection>(pres, XML_ROOT_NAME) ??
                new ConstantsCollection();

            Globals.PresentationEvents[pres].PresentationBeforeSave += OnPresentationBeforeSave;
        }

        void OnPresentationBeforeSave(object sender, PresentationBeforeSaveEventArgs e)
        {
            if (Constants.Count > 0)
                mSerializer.SerializeToPresentation(e.Presentation, Constants, XML_ROOT_NAME);
        }
    }
}
